package org.unt.logic;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XmlLogic {

	public static void main(String argv[]) {

		new XmlLogic().generate(getWasEightBean(), new File("./was8 3.xml"));
	}

	public void generate(WasEightBean bean, File xmlFile) {

		Document doc = createDocument();
		Element root = doc.createElement("x");
		doc.appendChild(root);

		addNewElementValue(bean, doc, root, "prd", "getPrd");
		addNewElementValue(bean, doc, root, "occ", "getOcc");
		addNewElementValue(bean, doc, root, "reflexName", "getReflexName");
		addNewElementValue(bean, doc, root, "aliasName", "getAliasName");

		Element appList = addNewElement(bean, doc, root, "applicationsList");
		Element app = addNewElement(bean, doc, appList, "application");

		addNewElementValue(bean, doc, app, "aliasName", "getAppAliasName");
		addNewElementValue(bean, doc, app, "source", "getAppSource");
		addNewElementValue(bean, doc, app, "contextRoot", "getAppContextRoot");
		addNewElementValue(bean, doc, app, "url", "getAppUrl");

		Element oracleList = addNewElement(bean, doc, app, "dsOracleList");
		Element oracle = addNewElement(bean, doc, oracleList, "dsOracle");

		addNewElementValue(bean, doc, oracle, "aliasName", "getOracleAliasName");
		addNewElementValue(bean, doc, oracle, "resRefName", "getOracleResRefName");
		addNewElementValue(bean, doc, oracle, "url", "getOracleUrl");
		addNewElementValue(bean, doc, oracle, "userKey", "getOracleUserKey");
		addNewElementValue(bean, doc, oracle, "passwordKey", "getOraclePasswordKey");
		addNewElementValue(bean, doc, oracle, "driverClassPath", "getOracleDriverClasspath");

		persistFile(doc, xmlFile);
	}

	private Element addNewElementValue(WasEightBean bean, Document doc, Element father, String elementName, String getter) {

		Element child = null;

		try {

			Method getterMethod = WasEightBean.class.getMethod(getter);
			String value = (String) getterMethod.invoke(bean);

			child = doc.createElement(elementName);
			child.appendChild(doc.createTextNode(value));

			father.appendChild(child);

		} catch (IllegalArgumentException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e);
		} catch (SecurityException e) {
			throw new RuntimeException(e);
		} catch (NoSuchMethodException e) {
			throw new RuntimeException(e);
		}

		return child;
	}

	private Element addNewElement(WasEightBean bean, Document doc, Element father, String elementName) {

		Element child = null;

		try {

			child = doc.createElement(elementName);
			father.appendChild(child);

		} catch (IllegalArgumentException e) {
			throw new RuntimeException(e);
		} catch (SecurityException e) {
			throw new RuntimeException(e);
		}

		return child;
	}

	private Document createDocument() {

		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder;

		try {

			docBuilder = docFactory.newDocumentBuilder();

		} catch (ParserConfigurationException e) {
			throw new RuntimeException(e);
		}

		return docBuilder.newDocument();

	}

	private void persistFile(Document doc, File xmlFile) {

		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer;

		try {

			transformer = transformerFactory.newTransformer();

		} catch (TransformerConfigurationException e) {
			throw new RuntimeException(e);
		}

		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(xmlFile);

		try {

			transformer.transform(source, result);

		} catch (TransformerException e) {
			throw new RuntimeException(e);
		}
	}

	static WasEightBean getWasEightBean() {

		WasEightBean bean = new WasEightBean();

		bean.setPrd("hwo");
		bean.setOcc("02");
		bean.setReflexName("HelloWorldOracle/D");
		bean.setAliasName("HelloWorldOracle");
		bean.setAppAliasName("App01");
		bean.setAppSource("HelloWorld.war");
		bean.setAppContextRoot("/");
		bean.setAppUrl("yval0130.inetpsa.com:8080");
		bean.setOracleAliasName("Ds01");
		bean.setOracleResRefName("jdbc/hwo_resref1");
		bean.setOracleUrl("jdbc:oracle:thin:@beu002.inetpsa.com:1521:HWO");
		bean.setOracleUserKey("hwouser");
		bean.setOraclePasswordKey("hwopassword");
		bean.setOracleDriverClasspath("%(homeDir)s/lib/ojdbc6.jar");

		return bean;
	}
}
